﻿namespace CKFinder.Connector
{
    using CKFinder;
    using CKFinder.Settings;
    using System;
    using System.IO;
    using System.Web;

    internal class FolderHandler
    {
        private int _AclMask;
        private string _ClientPath;
        private DirectoryInfo _FolderInfo;
        private ResourceType _ResourceTypeInfo;
        private string _ResourceTypeName;
        private string _ServerPath;
        private DirectoryInfo _ThumbsFolderInfo;
        private string _ThumbsServerPath;
        private string _Url;

        private FolderHandler(string resourceTypeName, string clientFolderPath)
        {
            this._ResourceTypeName = resourceTypeName;
            this._ClientPath = clientFolderPath;
            if (!this._ClientPath.EndsWith("/"))
            {
                this._ClientPath = this._ClientPath + "/";
            }
            if (!this._ClientPath.StartsWith("/"))
            {
                this._ClientPath = "/" + this._ClientPath;
            }
            this._AclMask = -1;
        }

        public bool CheckAcl(AccessControlRules aclToCheck)
        {
            return this.CheckAcl((int) aclToCheck);
        }

        public bool CheckAcl(int aclToCheck)
        {
            return ((this.AclMask & aclToCheck) == aclToCheck);
        }

        public static FolderHandler GetCurrent()
        {
            HttpRequest request = HttpContext.Current.Request;
            string resourceTypeName = request.QueryString["type"];
            if (resourceTypeName == null)
            {
                resourceTypeName = "";
            }
            string clientFolderPath = request.QueryString["currentFolder"];
            if (clientFolderPath == null)
            {
                clientFolderPath = "/";
            }
            return new FolderHandler(resourceTypeName, clientFolderPath);
        }

        public int AclMask
        {
            get
            {
                if (this._AclMask == -1)
                {
                    this._AclMask = Config.Current.AccessControl.GetComputedMask(this.ResourceTypeName, this.ClientPath);
                }
                return this._AclMask;
            }
        }

        public string ClientPath
        {
            get
            {
                return this._ClientPath;
            }
        }

        public DirectoryInfo FolderInfo
        {
            get
            {
                if (this._FolderInfo == null)
                {
                    this._FolderInfo = new DirectoryInfo(this.ServerPath);
                }
                return this._FolderInfo;
            }
        }

        public ResourceType ResourceTypeInfo
        {
            get
            {
                if (this._ResourceTypeInfo == null)
                {
                    this._ResourceTypeInfo = Config.Current.GetResourceTypeConfig(this.ResourceTypeName);
                    if (this._ResourceTypeInfo == null)
                    {
                        ConnectorException.Throw(12);
                    }
                }
                return this._ResourceTypeInfo;
            }
        }

        public string ResourceTypeName
        {
            get
            {
                return this._ResourceTypeName;
            }
        }

        public string ServerPath
        {
            get
            {
                if (this._ServerPath == null)
                {
                    this._ServerPath = Path.Combine(this.ResourceTypeInfo.GetTargetDirectory(), this.ClientPath.TrimStart(new char[] { '/' }));
                }
                return this._ServerPath;
            }
        }

        public DirectoryInfo ThumbsFolderInfo
        {
            get
            {
                if (this._ThumbsFolderInfo == null)
                {
                    this._ThumbsFolderInfo = new DirectoryInfo(this.ThumbsServerPath);
                }
                return this._ThumbsFolderInfo;
            }
        }

        public string ThumbsServerPath
        {
            get
            {
                if (this._ThumbsServerPath == null)
                {
                    this._ThumbsServerPath = Path.Combine(Config.Current.Thumbnails.GetTargetDirectory(), this.ResourceTypeInfo.Name);
                    this._ThumbsServerPath = Path.Combine(this._ThumbsServerPath, this.ClientPath.TrimStart(new char[] { '/' }));
                    Directory.CreateDirectory(this._ThumbsServerPath);
                }
                return this._ThumbsServerPath;
            }
        }

        public string Url
        {
            get
            {
                if (this._Url == null)
                {
                    this._Url = this.ResourceTypeInfo.Url + this.ClientPath.TrimStart(new char[] { '/' });
                }
                return this._Url;
            }
        }
    }
}

